package com.maker.ai.config;

import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.stdio.StdioMcpTransport;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class McpConfig {
    @Resource
    private OpenAiStreamingChatModel  openAiStreamingChatModel;

    @Value("${mcp.key}")
    private String key;
    @Value("\"${maker.api-key}")
    private String apiKey;


    /* ---------- 1. 命令执行服务 ---------- */
//    @Bean("cmdMcpClient")
//    public McpClient cmdMcpClient() {
//        McpTransport transport = new StdioMcpTransport.Builder()
//                .command(List.of("cmd", "/c", "npx", "-y",
//                        "@fsouza/mcp-server-command"))
//                .logEvents(true)
//                .build();
//        return new DefaultMcpClient.Builder()
//                .transport(transport)
//                .build();
//    }
    /* ---------- 2. 进程/端口服务 ---------- */
//    @Bean("procMcpClient")
//    public McpClient procMcpClient() {
//        McpTransport transport = new StdioMcpTransport.Builder()
//                .command(List.of("cmd", "/c", "npx", "-y",
//                        "@modelcontextprotocol/server-process"))
//                .logEvents(true)
//                .build();
//        return new DefaultMcpClient.Builder()
//                .transport(transport)
//                .build();
//    }

    /* ---------- 3. Docker 运维服务 ----------
    @Bean("dockerMcpClient")
    public McpClient dockerMcpClient() {
        // Windows 下 docker.sock 不存在，可改成 tcp://localhost:2375
        Map<String, String> env = new HashMap<>();
        env.put("DOCKER_HOST", "tcp://localhost:2375");
        McpTransport transport = new StdioMcpTransport.Builder()
                .command(List.of("cmd", "/c", "npx", "-y",
                        "@modelcontextprotocol/server-docker"))
                .environment(env)
                .logEvents(true)
                .build();
        return new DefaultMcpClient.Builder()
                .transport(transport)
                .build();
    }
    */
    /* ---------- 4. 百度地图服务（你已写完） ----------*/
    @Bean("baiduMapMcpClient")
    public McpClient baiduMapMcpClient() {
        McpTransport transport = new StdioMcpTransport.Builder()
                .command(List.of("cmd", "/c", "npx","-y","@baidumap/mcp-server-baidu-map"))
                .environment(Map.of("BAIDU_MAP_API_KEY", key))
                .logEvents(true) // only if you want to see the traffic in the log
                .build();
        return new DefaultMcpClient.Builder()
                .transport(transport)
                .build();
    }
    /*把网页搜索、新闻、爬取、站点地图、深度推理等在线能力一次性暴露成“工具”*/
    @Bean("maker_web_search")
    public McpClient makerWebSearch() {
        McpTransport transport = new StdioMcpTransport.Builder()
                .command(List.of("cmd", "/c", "npx","-y","search1api-mcp"))
                .environment(Map.of("SEARCH1API_KEY", apiKey))
                .logEvents(true) // only if you want to see the traffic in the log
                .build();
        return new DefaultMcpClient.Builder()
                .transport(transport)
                .build();
    }


    @Bean
    public McpToolProvider toolProvider(
//                                        @Qualifier("cmdMcpClient")     McpClient c1,
//                                        @Qualifier("procMcpClient")    McpClient c2,
//                                        @Qualifier("dockerMcpClient")  McpClient c3,
//                                        @Qualifier("maker_web_search") McpClient c5,
                                        @Qualifier("baiduMapMcpClient") McpClient c4){

        McpToolProvider  mcpToolProvider = McpToolProvider.builder()
            .mcpClients(List.of(c4))
            .build();
        return mcpToolProvider;
    }
//    @Bean
//    public McpChatService mcpChatService(McpToolProvider toolProvider){
//        return AiServices.builder(McpChatService.class)
//                .streamingChatModel(openAiStreamingChatModel)
//                .toolProvider(toolProvider)
//                .build();
//    }
}
